package initial;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import utility.IdDict;

/**
 * Sample trans data & rearrange ID according "activeUsers2.txt"
 * @author D
 *
 */

public class Rearrange2 {

	private static IdDict dict = new IdDict();
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String dictPath = "/home/ddxu/data/activeUsers2.txt";
		dict.loadDict(dictPath);

		String dataPath = "/home/ddxu/data/trans.txt";
		String resultPath = "/home/ddxu/data/newTrans2.txt";
		
		String str;
		String[] tokens;
		Integer buyer_id, seller_id;
		
		try {
			BufferedReader in = new BufferedReader(new FileReader(dataPath));
			BufferedWriter out = new BufferedWriter(new FileWriter(resultPath));
			while((str = in.readLine()) != null) {
				tokens = str.split("\t");
				buyer_id = dict.dictValue(Integer.parseInt(tokens[1]));
				seller_id = dict.dictValue(Integer.parseInt(tokens[2]));
				
				if((buyer_id != null) && (seller_id != null)) {
					tokens[1] = buyer_id.toString();
					tokens[2] = seller_id.toString();
					for(int i = 0; i < 9; i++) {
						out.write(tokens[i]);
						out.write("\t");
					}
					out.newLine();
				}
			}
			in.close();
			out.flush();
			out.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
